<template>
  <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in employess" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="$emit('hEsc')">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { postDepartments, getDepartDetail, putDepar } from '@/api/departments'
import { getSimpleEmployees } from '@/api/employees'
import { is } from 'type-is'

export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // rule 规则对象
    // value 要检验的值
    // callback 回调
    const validCode = (rule, value, callback) => {
      let codeList = null
      if (this.isEdit) {
        // 找到除了自己其他部门的 code
        codeList = this.originList.filter(item => item.id !== this.id).map(({ code }) => code)
      } else {
        codeList = this.originList.map(({ code }) => code)
      }
      codeList.includes(value) ? callback(new Error('编码' + '已存在')) : callback()
    }
    const validName = (rule, value, callback) => {
      let nameList = null
      if (is.isEdit) {
        const pid = this.originList.find(item => item.id === this.id).pid
        nameList = this.originList.filter(item => item.pid === pid && item.id !== this.id).map(({ name }) => name)
      } else {
        nameList = this.originList.filter(item => item.pid === this.id).map(({ name }) => name)
      }
      nameList.includes(value) ? callback(new Error('名称已经存在')) : callback()
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      employess: []
    }
  },
  created() {
    this.loadSimple()
    this.loadDetail()
  },
  methods: {
    // 获取简单员工信息
    async loadSimple() {
      try {
        const res = await getSimpleEmployees()
        this.employess = res.data
      } catch (e) {
        console.log(e)
      }
    },
    // 数据回填获取部门详细信息
    async loadDetail() {
      if (!this.isEdit) return
      const res = await getDepartDetail(this.id)
      this.form = res.data
    },
    async hSubmit() {
      this.$refs.deptForm.validate(async valid => {
        if (!valid) return
        try {
          let res = null
          if (this.isEdit) {
            res = await putDepar(this.form)
          } else {
            res = await postDepartments({ ...this.form, pid: this.id })
          }
          this.$message.success(res.message)
          this.$emit('success')
        } catch (e) {
          this.$message.error(e.message)
        }
      })
    }
  }
}
</script>

<style>

</style>
